/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "PropertyWindow.h"

namespace PQBuilder
{
	PropertyWindow::PropertyWindow(QWidget *parent)
		: WidgetBase(parent),_renderControl(0)
	{
		_toolBox = new QToolBox(this);
		QVBoxLayout* box=new QVBoxLayout(this);
		
		box->addWidget(_toolBox);
		this->setLayout(box);
		this->setMinimumWidth(230);
		
	}

	PropertyWindow::~PropertyWindow()
	{

	}

	void PropertyWindow::OnComponentAttached(PQEngine::Component* component)
	{
		removeAllComponentsUI();

		if(!component){
			return;
		}
		ComponentControl* control=new ComponentControl(component);
		_toolBox->addItem(control,control->getComponentText());
		_controls.push_back(control);
	}

	void PropertyWindow::OnComponentDetached(PQEngine::Component* component)
	{
	}


	void PropertyWindow::removeAllComponentsUI()
	{
		int cnt=_toolBox->count();
		for(int i=0;i<cnt;i++){
			_toolBox->removeItem(i);
			
		}
		cnt=_controls.size();
		for(int i=0;i<cnt;i++){
			if(_controls[i]){
				delete _controls[i];
			}
		}
		_controls.clear();
	}

	void PropertyWindow::OnGameObjectSelected(PQEngine::GameObject *object)
	{
		removeAllComponentsUI();
		
		if(!object){
			return;
		}
		
		int size=object->getComponentCount();
		for(int i=0;i<size;i++){
			PQEngine::Component* component=object->getComponent(i);
			ComponentControl* control=new ComponentControl(component);
			_toolBox->addItem(control,control->getComponentText());
			_controls.push_back(control);
		}

	}

	void PropertyWindow::OnGameObjectDeselected(PQEngine::GameObject *object)
	{
		if(!object){
			return;
		}
		removeAllComponentsUI();
	}

	void PropertyWindow::OnRenderPropertySelected()
	{
		removeAllComponentsUI();
		if(!_renderControl){
			_renderControl=new RenderSettingControl();
		}
		_toolBox->addItem(_renderControl,QObject::tr("Render Properties"));
	}

}
